
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

import java.util.*;

import javax.swing.Timer;


public class RecycleGame extends JApplet implements Runnable, MouseListener,
		MouseMotionListener, ActionListener {
	/**
	 * version 1
	 */
	private static final long serialVersionUID = 1L;
	int oX1 = 150;
	int oY1 = 200;
	Thread thread;
	Font f;
	int score,leveln=1;
	boolean count=true;
	String scoreS = "Score: " + score;
	String level = "Level: " + leveln;
	String Paper,Organic,Household,Plastic,Glass;
	Image object1,person,blueBin,brownBin, greyBin, greenBin,yellowBin,bg,intro;
	int oCount;
	Random rand = new Random();
	boolean bonus;
	boolean start=false;
	
	int mX,toX1,toY1;
	int mY;
	int dx, dy;
	int xMoved, yMoved;
	
	Timer timer;

	@Override
	public void init() {
		setSize(800, 600);
		
		addMouseListener(this);
		addMouseMotionListener(this);
		f = new Font("TimesRoman", Font.BOLD, 24);
		intro = getImage(getCodeBase(),"intro.png");
		object1 = getImage(getCodeBase(), "apple.png");
		timer = new Timer(25, this);
		bonus=false;
		count=true;
		score=0;
		leveln=1;
	}

	@Override
	public void start() {
		thread = new Thread(this);
	}

	@Override
	public void run() {
	}

	@Override
	public void stop() {

	}

	public void destroy() {
		
	}

	public void paint(Graphics g) {
		g.setFont(f);
		g.clearRect(0, 0, 800, 600);
		if(start==false){
			g.drawImage(intro, 0, 0, this);
		}
		else{
			
			person = getImage(getCodeBase(), "pane_run_person.png");
			blueBin = getImage(getCodeBase(), "blue_container.png");
			brownBin = getImage(getCodeBase(), "brown_container.png");
			greenBin = getImage(getCodeBase(), "green_container.png");
			greyBin = getImage(getCodeBase(), "grey_container.png");
			yellowBin = getImage(getCodeBase(), "yellow_container.png");
			bg =getImage(getCodeBase(), "background.png");
			if(leveln==1){
				Paper = "Paper";
				Organic = "Organic";
				Glass = "Glass";
				Plastic = "Plastic";
				Household = "Household";
			}
			else if(leveln>=2&&bonus!=true){
				Paper = " ";
				Organic =" ";
				Glass = " ";
				Plastic = " ";
				Household = " ";
			}
			else{
			
				blueBin = null;
				brownBin = null;
				greenBin = getImage(getCodeBase(), "abst.png");
				greyBin = getImage(getCodeBase(), "tct.png");
				yellowBin = null;
			}
			g.drawImage(bg, 0, 0, this);
			g.drawString(level,20,50);
			g.drawString(scoreS, 200, 50);
			
			g.drawImage(blueBin, 580, 400, this);
			g.drawString(Paper, 700, 450);
			g.drawImage(brownBin, 500, 100, this);
			g.drawString(Organic, 630, 150);
			g.drawImage(greenBin, 480, 250, this);
			g.drawString(Glass, 600, 300);
			g.drawImage(greyBin, 480, 0, this);
			g.drawString(Household, 600, 50);
			g.drawImage(yellowBin, 480, 500, this);
			g.drawString(Plastic, 600, 550);
			g.drawImage(person, 0, 200, this);
			g.drawImage(object1, oX1, oY1, this);
		}
		
	}


	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent e) {

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		oCount = rand.nextInt(11);
		if (oCount==1){
			object1 = getImage(getCodeBase(),"cardboard.png");
		}else if(oCount == 2){
		object1 = getImage(getCodeBase(), "bitten_apple_icon.png");}
		else if(oCount==3){
			object1 = getImage(getCodeBase(), "apple.png");
		}
		else if(oCount== 4){
			object1 = getImage(getCodeBase(), "coke.png");
		}
		else if(oCount== 5){
			object1 = getImage(getCodeBase(), "dcoke.png");
		}
		else if(oCount== 6){
			object1 = getImage(getCodeBase(), "news.png");
		}
		else if(oCount== 7){
			object1 = getImage(getCodeBase(), "pen.png");
		}
		else if(oCount== 8){
			object1 = getImage(getCodeBase(), "pencil.png");
		}
		else if(oCount ==9){
			object1 = getImage(getCodeBase(), "cotton.png");
		}
		else if(oCount ==10){
			object1 = getImage(getCodeBase(), "glass_bottle.png");
		}

		else{
			object1 = getImage(getCodeBase(), "glass.png");
		}

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		// thread.start();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
		mX = e.getX();
		mY = e.getY();
		dx = mX - oX1;
		dy = mY - oY1;
		if(start==false){
			start=true;
			
		}
		else{
			if(bonus==false){
				blueBin = getImage(getCodeBase(), "blue_container.png");
				brownBin = getImage(getCodeBase(), "brown_container.png");
				greenBin = getImage(getCodeBase(), "green_container.png");
				greyBin = getImage(getCodeBase(), "grey_container.png");
				yellowBin = getImage(getCodeBase(), "yellow_container.png");
			if((object1 == getImage(getCodeBase(), "bitten_apple_icon.png")||
					object1 == getImage(getCodeBase(), "apple.png"))&&
					(mX>550&&mX<610&&mY>100&&mY<190)){
					brownBin = getImage(getCodeBase(), "brcg.png");
					score+=10;
					
					if(score==100){
						JOptionPane.showMessageDialog(getContentPane(), "Congratulations! You have made it to level2!");
					}
			}//brown done

			else if((object1==getImage(getCodeBase(),"cardboard.png")||
					object1 == getImage(getCodeBase(), "news.png"))&&
					(mX>609&&mX<699&&mY>400&&mY<587)){
					blueBin = getImage(getCodeBase(), "bcg.png");
					score+=10;
				
					if(score==100){
						JOptionPane.showMessageDialog(getContentPane(), "Congratulations! You have made it to level2!");
					}
				
			}//blue done
			else if((object1==getImage(getCodeBase(),"coke.png")||
					object1==getImage(getCodeBase(),"dcoke.png"))&&
					(mX>510&&mX<595&&mY>500&&mY<583)){
					yellowBin = getImage(getCodeBase(), "ycg.png");
					score+=10;
					
					if(score==100){
						JOptionPane.showMessageDialog(getContentPane(), "Congratulations! You have made it to level2!");
					}
				
			}//yellow
			else if((
					object1==getImage(getCodeBase(),"pencil.png")||
					object1==getImage(getCodeBase(),"cotton.png")||
							object1==getImage(getCodeBase(),"pen.png"))&&
							(mX>520&&mX<600&&mY>0&&mY<85)){
				greyBin = getImage(getCodeBase(), "grcg.png");
					score+=10;
					
					if(score==100){
						JOptionPane.showMessageDialog(getContentPane(), "Congratulations! You have made it to level2!");
					}
				
			}//white
			else if((object1==getImage(getCodeBase(),"glass.png")||
					object1==getImage(getCodeBase(),"glass_bottle.png"))
					&&(mX>523&&mX<593&&mY>250&&mY<340)){
					greenBin = getImage(getCodeBase(), "gcg.png");

					score+=10;
					if(score==100){
						JOptionPane.showMessageDialog(getContentPane(), "Congratulations! You have made it to level2!");
					}
				
			}//green done
			else{
				score-=10;
				count=false;
				
			}}
			
			else{//atlanta game!
				blueBin = null;
				brownBin = null;
				greenBin = getImage(getCodeBase(), "abst.png");
				greyBin = getImage(getCodeBase(), "tct.png");
				yellowBin = null;
				if(object1 == getImage(getCodeBase(), "bitten_apple_icon.png")||
					object1 == getImage(getCodeBase(), "apple.png")||
					object1==getImage(getCodeBase(),"cotton.png")||
					object1==getImage(getCodeBase(),"pencil.png")||
					object1==getImage(getCodeBase(),"pen.png")
					){
					
				}
				else{
					if(mX>485&&mX<668&&mY>300&&mY<545){
						greenBin = getImage(getCodeBase(),"abstg.png");
				score+=5;}
				}
			}
			timer.start();
			scoreS = "Score: " + score;
			leveln = score/100+1;
			level = "Level: "+leveln;
			
			if(score<0){
				JOptionPane.showMessageDialog(getContentPane(), "You Lost!");
				JOptionPane.showMessageDialog(getContentPane(), "Go to http://recycleright.weebly.com/barcelona.html to learn how to separate!");
				score=0;
				scoreS = "Score: " + score;
				

			}
			else if(score==200&&count==true){
				JOptionPane.showMessageDialog(getContentPane(), "Good job! You have entered the bonus round!");
				bonus=true;
				count=false;
				

			}
			else if(score==300){
				JOptionPane.showMessageDialog(getContentPane(), "Congratulations! You won!");
				score=0;
				scoreS = "Score: " + score;
				leveln=1;
				level = "Level: "+leveln;
				init();
				
			}
		
		}

	
		repaint();
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		oX1 += dx/10.0;
		oY1 += dy/10.0;
		
		xMoved += Math.abs(dx/10.0);
		yMoved += Math.abs(dy/10.0);
		repaint();
		if (xMoved >0.9*dx && yMoved > 0.9*dy){
			timer.stop();
			oX1=150;
			oY1=200;
			xMoved = 0;
			yMoved = 0;
		}

		
		
	}


}
